<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepClone</title>
</head>
<body>
    <script>
        function deepClone(obj, map = new WeakMap()){
            if(typeof obj == 'object'){
                var isArray = Object.prototype.toString.call(obj) == '[object Array]';
                var cloneObj = isArray ? [] : {};
                if(map.get(obj)){
                    return map.get(obj);
                }
                map.set(obj, cloneObj);
                for (const key in obj) {
                    if (obj.hasOwnProperty(key)) {
                        if(typeof obj[key] == 'object'){
                            cloneObj[key] = deepClone(obj[key], map);
                        }else{
                            cloneObj[key] = obj[key];
                        }
                    }
                }
                return cloneObj;
            }else{
                return obj;
            }
        }
        var a = {
            a: 1,
            b: 2,
            c: [1,2,3,4]
        };
        a.target = a;
        var b = deepClone(a);
        b.a = 2;
        console.log(b);
    </script>
</body>
</html>